// ttBop
// 2013.05.16
// _nderscore

(function(){
  if(typeof ttBop !== 'undefined') ttBop.cleanup();

  ttBop = {

    el:null,
    onoff:1,
    boptimer:0,
    idletimer:0,
    room:(function(){
            for(var a in turntable) {
              var b = turntable[a];
              if(typeof b !== 'object' || b === null) continue;
              if(typeof b.setupRoom !== 'undefined') return b;
            }
            return false;
          })(),
    api: (function(){
            var reg = / Preparing message /i;
            for(var a in turntable) {
              var b = turntable[a];
              if(typeof b !== 'function') continue;
              b.toString = Function.prototype.toString;
              if(reg.test(b.toString())) return b;
            }
            return false;
          })(),

    scheduleBop:function(){
      var r = Math.floor(Math.random() * (60000 - 10000 + 1)) + 10000;
      ttBop.boptimer = setTimeout(ttBop.bop,r);
    },

    bop:function(){
      turntable.whenSocketConnected(function(){
        if(ttBop.room.currentSong){
          ttBop.api({
            api: 'room.vote',
            roomid: ttBop.room.roomId,
            val: 'up',
            vh: $.sha1(ttBop.room.roomId+'up'+ttBop.room.currentSong._id),
            th: $.sha1(Math.random() + ''),
            ph: $.sha1(Math.random() + '')
          });
        }
      });
    },

    evtListener:function(_){
      if(ttBop.onoff && _.command && _.command == 'newsong')
        ttBop.scheduleBop();
    },

    toggle:function(){
      ttBop.onoff = ttBop.onoff ^ 1;

      if(!ttBop.onoff) clearTimeout(ttBop.boptimer);
      else ttBop.bop();

      ttBop.el.html('Autobop: <span>'+(ttBop.onoff?'ON':'OFF')+'</span>');
      ttBop.el.find('span').css({
        'color':(ttBop.onoff?'#0F0':'#F00'),
        'font-weight':'bold'
      });
    },

    cancel:function(){
      clearTimeout(ttBop.boptimer);
    },

    cleanup:function(){
      ttBop.el.unbind('click');
      ttBop.el.remove();
      clearTimeout(ttBop.boptimer);
      clearTimeout(ttBop.idletimer);
      $('#awesome-button, #lame-button').unbind('click',ttBop.cancel);
      turntable.removeEventListener('message', ttBop.evtListener);
      console.log('Cleaned up my mess!');
    },

    init:function(){
      // setup menu option
      ttBop.el = $('<li class="option">Autobop: <span>ON</span></li>');
      ttBop.el.find('span').css({
        'color':'#0F0',
        'font-weight':'bold'
      });
      ttBop.el.insertAfter($('#settings li:contains(Ignored)'));
      ttBop.el.bind('click',ttBop.toggle);

      // disable if the user votes before autobop fires
      $('#awesome-button, #lame-button').bind('click',ttBop.cancel);

      // setup anti-idle
      ttBop.idletimer = setInterval(function(){
        $("form input:last").eq(0).keydown();
      }, 100000);

      // setup event listener
      turntable.addEventListener('message', ttBop.evtListener);

      ttBop.bop();

      console.log('ttBop By _nderscore is loaded!');
    }

  };

  ttBop.init();

})();